package com.atguigu.nio.channel;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

public class NIOFileChannel02 {
    public static void main(String[] args) {

        File file = new File("d:\\file01.txt");

        try (//创建文件的输入流
             FileInputStream fileInputStream = new FileInputStream(file);
             //通过fileInputStream 获取对应的FileChannel -> 实际类型  FileChannelImpl
             FileChannel fileChannel = fileInputStream.getChannel()) {

            //创建缓冲区
            ByteBuffer byteBuffer = ByteBuffer.allocate((int) file.length());

            //将 通道的数据读入到Buffer
            fileChannel.read(byteBuffer);

            //将byteBuffer 的 字节数据 转成String
            System.out.println(new String(byteBuffer.array()));

        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }
}
